<?php
/**
 * Created by PhpStorm.
 * User: xding
 * Date: 15/10/26
 * Time: 下午3:29
 */

class Uploadfile extends MY_Controller {

    /**
     * 响应CKEditor上传图片
     */
    public function ck_upload_img () {

        // 回调值
        $ckeditor_funcnum = $this->input->get('CKEditorFuncNum');

        // form field -----> upload
        $config['upload_path']      = './public/resource/uploads/images/';
        $config['allowed_types']    = 'gif|jpg|png';

        $this->load->library('upload', $config);

        if ( ! $this->upload->do_upload('upload'))
        {
            $error = array('error' => $this->upload->display_errors());
            echo "<script>alert('上传失败');</script>";
            die();
        }
        else
        {
            // 此处传回的是图片的URL地址
            $data = array('upload_data' => $this->upload->data());
            $img_url = base_url('public/resource/uploads/images') . '/' . $data['upload_data']['file_name'];
            echo "<script>window.parent.CKEDITOR.tools.callFunction('" . $ckeditor_funcnum . "','" . $img_url . "', '')</script>";
            die();
        }
    }

    public function _upload() {

        $this->load->library('uploadautoloader');


        $storage = new \Upload\Storage\FileSystem('./uploads');
        // $file = new \Upload\File('foo', $storage);
        die('qqq');

        // Optionally you can rename the file on upload
        $new_filename = uniqid();
        $file->setName($new_filename);

        // Validate file upload
        // MimeType List => http://www.iana.org/assignments/media-types/media-types.xhtml
        $file->addValidations(array(
            // Ensure file is of type "image/png"
            new \Upload\Validation\Mimetype('image/png'),

            //You can also add multi mimetype validation
            //new \Upload\Validation\Mimetype(array('image/png', 'image/gif'))

            // Ensure file is no larger than 5M (use "B", "K", M", or "G")
            new \Upload\Validation\Size('5M')
        ));

        // Access data about the file that has been uploaded
        $data = array(
            'name'       => $file->getNameWithExtension(),
            'extension'  => $file->getExtension(),
            'mime'       => $file->getMimetype(),
            'size'       => $file->getSize(),
            'md5'        => $file->getMd5(),
            'dimensions' => $file->getDimensions()
        );

        // Try to upload file
        try {
            // Success!
            // $file->upload();
        } catch (\Exception $e) {
            // Fail!
            $errors = $file->getErrors();
        }

    }

}